package com.htu.tax.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.htu.tax.common.ResponseHelper;
import com.htu.tax.common.ResponseModel;
import com.htu.tax.entity.TInvoice;
import com.htu.tax.mapper.TInvoiceMapper;
import com.htu.tax.payload.request.Invoice.addRequest;
import com.htu.tax.payload.request.Invoice.showRequest;
import com.htu.tax.payload.request.Invoice.update1Request;
import com.htu.tax.payload.request.Invoice.updateRequest;
import com.htu.tax.payload.base.PageRequest;
import com.htu.tax.service.ITInvoiceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 */
@Service
public class TInvoiceServiceImpl extends ServiceImpl<TInvoiceMapper, TInvoice> implements ITInvoiceService {
    @Resource
    private TInvoiceMapper mapper;


    @Override
    public void updateState(updateRequest request) {
        // TInvoice invoice = this.getById(Long.valueOf(request.getId()));
        // BeanUtils.copyProperties(request,invoice);
        //invoice.setCreateTime(LocalDateTime.now());
        request.setAgreeTime(LocalDateTime.now());
        mapper.updateState(request);
    }

    @Override
    public void updateState1(update1Request request) {
        // TInvoice invoice = this.getById(Long.valueOf(request.getId()));
        // BeanUtils.copyProperties(request,invoice);
        //invoice.setCreateTime(LocalDateTime.now());
        request.setRefuseTime((LocalDateTime.now()));
        mapper.updateState1(request);
    }


    @Override
    public ResponseModel addInvoice(addRequest request) {
        TInvoice invoice = new TInvoice();
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);

        //判断用户名是否存在
        Integer count = mapper.isHaveUser(invoice.getId());
        if (count != 0) {
            return ResponseHelper.error("用户已存在");
        }
        BigDecimal a = request.getTaxRate();

        BigDecimal b = request.getExcludingTaxMoney();

        BigDecimal tax = a.multiply(b);

        BigDecimal addStr = tax.add(b);
        // String address= mapper.getAddress();
        // Long driverid = mapper.getDriverId();
        //Long companyid=mapper.getCompanyId();

        //关联？半成品
        invoice.setId(snowflake.nextId())

                .setInvoiceNum(request.getInvoiceNum())

                .setState(request.getState())
                // .setDriverId(driverid);

                .setDriverId(request.getDriverId())
                //.setCompanyId(companyid)
                .setCompanyId(request.getCompanyId())

                .setTaxpayer(request.getTaxpayer())

                .setTotalMoney(addStr)

                .setTaxRate(a)

                .setTax(tax)

                .setExcludingTaxMoney(b)
                // .setAddress(address);
                .setAddress(request.getAddress())

                .setCreateTime(LocalDateTime.now());

        // .setAgreeTime(invoice.getAgreeTime());


        mapper.insert(invoice);

        return ResponseHelper.buildResponseModel("添加成功");

    }

    @Override
    public IPage<TInvoice> show(showRequest request) {
        IPage<TInvoice> tInvoiceIPage = mapper.show(PageRequest.getPage(request), request.getState());
        return tInvoiceIPage;
    }
}

